﻿using System;
using System.Runtime.Serialization;
using NativeCode.Core.Data;
using NativeCode.Core.Extensions;

namespace NativeCode.Web.Controls.Filters
{
	[DataContract]
	[Serializable]
	public class QueryFilterBarItem
	{
		public QueryFilterBarItem()
		{
		}

		public QueryFilterBarItem(Type type, string filterPropertyName, QueryFilterOperator queryFilterOperator, object filterValue,
		                          QueryFilterConjunction filterConjunction)
		{
			DisplayFormat = "{0}";
			SelectedFormat = "{0}";

			FilterDisplayText = filterPropertyName.ToDisplayText();
			FilterConjunction = filterConjunction;
			FilterKey = QueryFilter.GetFilterKey(type, filterPropertyName, queryFilterOperator);
			FilterOperator = queryFilterOperator;
			FilterPropertyName = filterPropertyName;
			FilterValue = filterValue;
		}

		/// <summary>
		/// Used by the UI to show the default display state when nothing is selected.
		/// </summary>
		public string DisplayText
		{
			get
			{
				return (!string.IsNullOrEmpty(DisplayFormat))
				       	? string.Format(DisplayFormat, FilterDisplayText)
				       	: string.Empty;
			}
		}

		/// <summary>
		/// Used by the UI to show what was selected and it's filter value.
		/// </summary>
		public string SelectedText
		{
			get
			{
				return (!string.IsNullOrEmpty(SelectedFormat) && !string.IsNullOrEmpty(FilterValueText))
				       	? string.Format(SelectedFormat, FilterValueText.ToDisplayText())
				       	: string.Empty;
			}
		}

		public string FilterValueText
		{
			get { return (FilterValue != null) ? FilterValue.ToString() : string.Empty; }
		}

		[DataMember]
		public string DisplayFormat { get; set; }

		[DataMember]
		public string SelectedFormat { get; set; }

		[DataMember]
		public string FilterDisplayText { get; set; }

		[DataMember]
		public QueryFilterConjunction FilterConjunction { get; set; }

		[DataMember]
		public QueryFilterOperator FilterOperator { get; set; }

		[DataMember]
		public string FilterKey { get; set; }

		[DataMember]
		public string FilterPropertyName { get; set; }

		[DataMember]
		public object FilterValue { get; set; }
	}
}